# coding:utf-8
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from util.get_by_local import GetByLocal
from base.base_driver import BaseDriver
class ActionMethod:
    def __init__(self):
        base_driver = BaseDriver()
        self.driver = base_driver.android_driver(0)
        self.get_by_local = GetByLocal(self.driver)
    def input(self,*args):
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.clear()
        element.send_keys(args[1])

    def on_click(self, *args):
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return args[0], "元素没找到"
        element.click()

    def sleep_time(self, *args):
        time.sleep(int(args[0]))

    def get_size(self, *args):
        size = self.driver.get_window_size()  # 获取屏幕的width和height
        width = size['width']
        height = size['height']
        return width, height

    # 向左滑动
    def swipe_left(self, *args):
        x1 = self.get_size()[0] / 10 * 9
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10
        self.driver.swipe(x1, y1, x, y1, )

    # 向右滑动
    def swipe_right(self, *args):
        x1 = self.get_size()[0] / 10 * 9
        y1 = self.get_size()[1] / 2
        x = self.get_size()[0] / 10
        self.driver.swipe(x, y1, x1, y1)

    # 向上滑动
    def swipe_up(self, *args):
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10 * 9
        y = self.get_size()[1] / 10
        self.driver.swipe(x1, y1, x1, y)

    # 向下滑动
    def swipe_down(self, *args):
        x1 = self.get_size()[0] / 2
        y1 = self.get_size()[1] / 10 * 9
        y = self.get_size()[1] / 10
        self.driver.swipe(x1, y, x1, y1, )

    def swipe_on(self, *args):
        if args[0] == 'left':
            self.swipe_left()
        elif args[0] == 'right':
            self.swipe_right()
        elif args[0] == 'up':
            self.swipe_up()
        else:
            self.swipe_down()

    def get_element(self, *args):
        element = self.get_by_local.get_element(args[0])
        if element == None:
            return None
        return element

    def get_tost_element(self, *args):
        '''
        获取tostelement
        '''
        time.sleep(2)
        tost_element = ("xpath", "//*[contains(@text," + args[0] + ")]")
        return WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_element_located(tost_element))